package com.huang.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.entity.ArticleTag;
import com.huang.entity.Tag;
import com.huang.mapper.ArticleTagMapper;
import com.huang.mapper.TagMapper;
import com.huang.model.PageResult;
import com.huang.model.request.ArticleConditionQuery;
import com.huang.model.request.PageQuery;
import com.huang.model.request.TagReq;
import com.huang.model.response.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * 标签服务
 *
 * @author Ikaros
 * @since 2025/9/9 18:21 星期二
 */
@Service
@RequiredArgsConstructor
public class TagService extends ServiceImpl<TagMapper, Tag> {

    private final ArticleTagMapper articleTagMapper;

    private final ArticleTagMapper articleMapper;

    /**
     * 查询文章列表
     *
     * @param articleConditionQuery 文章查询条件
     * @return 文章列表
     */
    public ArticleConditionList listArticleTag(ArticleConditionQuery articleConditionQuery) {
        List<ArticleConditionResp> articleConditionList = articleMapper.selectArticleListByCondition(articleConditionQuery);
        String name = baseMapper.selectOne(new LambdaQueryWrapper<Tag>()
                        .select(Tag::getTagName)
                        .eq(Tag::getId, articleConditionQuery.getTagId()))
                .getTagName();
        return ArticleConditionList.builder()
                .articleConditionVOList(articleConditionList)
                .name(name)
                .build();
    }

    /**
     * 查询标签列表
     *
     * @return 标签列表
     */
    public List<TagResp> listTagVO() {
        return baseMapper.selectTagVOList();
    }

    /**
     * 查询标签选项列表
     *
     * @return 标签选项列表
     */
    public List<TagOptionResp> listTagOption() {
        return baseMapper.selectTagOptionList();
    }

    /**
     * 修改标签
     *
     * @param tag 标签信息
     */
    public void updateTag(TagReq tag) {
        // 标签是否存在
        Tag existTag = baseMapper.selectOne(new LambdaQueryWrapper<Tag>()
                .select(Tag::getId)
                .eq(Tag::getTagName, tag.getTagName()));
        Assert.isTrue(Objects.isNull(existTag) || existTag.getId().equals(tag.getId()),
                tag.getTagName() + "标签已存在");
        // 修改标签
        Tag newTag = Tag.builder()
                .id(tag.getId())
                .tagName(tag.getTagName())
                .build();
        baseMapper.updateById(newTag);
    }

    /**
     * 删除标签
     *
     * @param tagIdList 标签id列表
     */
    public void deleteTag(List<Integer> tagIdList) {
        // 标签下是否有文章
        Long count = articleTagMapper.selectCount(new LambdaQueryWrapper<ArticleTag>()
                .in(ArticleTag::getTagId, tagIdList));
        Assert.isTrue(count <= 0, "删除失败，标签下存在文章");
        // 批量删除标签
        baseMapper.deleteByIds(tagIdList);
    }

    /**
     * 添加标签
     *
     * @param tag 标签信息
     */
    public void addTag(TagReq tag) {
        // 标签是否存在
        Tag existTag = baseMapper.selectOne(new LambdaQueryWrapper<Tag>()
                .select(Tag::getId)
                .eq(Tag::getTagName, tag.getTagName()));
        Assert.isNull(existTag, tag.getTagName() + "标签已存在");
        // 添加新标签
        Tag newTag = Tag.builder()
                .tagName(tag.getTagName())
                .build();
        baseMapper.insert(newTag);
    }

    /**
     * 分页查询标签列表
     *
     * @param tagQuery 标签查询条件
     * @return 分页查询结果
     */
    public PageResult<TagBackResp> listTagBackVO(PageQuery tagQuery) {
        // 查询标签数量
        List<TagBackResp> tagBackRespList = baseMapper.selectBackTagList(tagQuery);
        if (tagBackRespList.isEmpty()) {
            return new PageResult<>();
        }
        return new PageResult<>(tagBackRespList, tagBackRespList.getFirst().getTotalCount());
    }
}
